package com.mianyang.zhangsan.common.util;

import com.mianyang.zhangsan.model.entity.SecRole;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
 * @author jiangjr
 * @date 2022-11-24
 */
@Slf4j
public class DateHelper {
    private static final Calendar CALENDER = Calendar.getInstance();

    public static String formatSecond(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    public static Date parseSecond(String date) {
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
        } catch (ParseException e) {
            log.error("时间转化异常");
        }
        return parse;
    }

    public static String containKey(Predicate<Map<String, Object>> condition) {
        Map<String, Object> map = new HashMap<>(16);
        String hello = "";
        map.put("hello", "java");
        if (condition.test(map)) {
            hello = (String) map.get("hello");
        }
        return hello;
    }

    public static Date parseDate(String time, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date parse = new Date();
        if (StringUtils.isNotEmpty(time)) {
            try {
                parse = format.parse(time);
            } catch (ParseException e) {
                // do nothing
            }
        }
        return parse;
    }

    public static Date preYear(String target) {
        return changeYear(target, -1, "yyyy");
    }

    public static Date nextYear(String target) {
        return changeYear(target, 1, "yyyy");
    }

    public static Date changeYear(String target, int year, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date parse = null;
        try {
            parse = format.parse(target);
        } catch (ParseException e) {
            parse = new Date();
        }
        return changeYear(parse, year);
    }

    public static Date changeYear(Date target, int year) {
        return changeDate(target, Calendar.YEAR, year);
    }

    public static Date changeDate(Date target, int field, int year) {
        CALENDER.setTime(target);
        CALENDER.add(field, year);
        return CALENDER.getTime();
    }

    public static void main(String[] args) {
//        File file = new File("D:\\software\\wechat\\data\\WeChat Files\\wxid_qkihded1418b22\\FileStorage\\Video\\2023-04\\教育.mp4");
//        long duration = VideoUtils.getVideoDuration(file);
//        System.out.println(duration);
    }
}
